package JDBC基础练习;

import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

//删除练习
public class testJDBC4 {
    public static void main(String[] args) throws SQLException {
        //1.创建DataSource
        DataSource dataSource=new MysqlDataSource();
        ((MysqlDataSource)dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/java100?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("suojie123");

        //2.建立连接
        Connection connection=dataSource.getConnection();

        //3.构造SQL语句
        //  删除一条记录：根据用户输入的id删除对应的学生记录
        System.out.println("请输入要删除的学生id");
        Scanner scanner = new Scanner(System.in);
        int id=scanner.nextInt();

        String sql="delete from student where id=?";
        PreparedStatement statement=connection.prepareStatement(sql);
        statement.setInt(1,id);

        //4.执行SQL
        int ret=statement.executeUpdate();
        System.out.println("ret="+ret);

        //5.释放资源
        statement.close();
        connection.close();



    }
}
